package com.ramos.kuaixiao.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.entity.WareHouse;

import java.util.List;
import java.util.Set;

public class WareAdapter extends BaseAdapter {

    private List<WareHouse> tasklist;
    private Context context;

    static class ViewHolder {
        TextView tvName;
        ImageView tvImg;
        LinearLayout ware_ll;
    }

    public WareAdapter(Context context, List<WareHouse> hotcourse) {
        this.context = context;
        this.tasklist = hotcourse;
    }

    public List<WareHouse> getWareList() {
        return tasklist;
    }

    public void refresh(List<WareHouse> data) {
        tasklist = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return tasklist.size();
    }

    @Override
    public Object getItem(int position) {
        return tasklist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.listitem_ware, null);
            holder = new ViewHolder();
            holder.tvName = (TextView) convertView.findViewById(R.id.ware_name);
            holder.tvImg = (ImageView) convertView.findViewById(R.id.ware_choose);
            holder.ware_ll = (LinearLayout) convertView.findViewById(R.id.ware_ll);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvName.setText(tasklist.get(position).getName());
        Log.d("ware", "tasklist.get(position).isUsed " + tasklist.get(position).isUsed + position + " " + tasklist.get(position).getName());
        if (tasklist.get(position).isUsed) {
            holder.tvImg.setBackgroundResource(R.drawable.ic_tick_blue);
        } else {
            holder.tvImg.setBackgroundResource(R.drawable.white);
        }
        holder.ware_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tasklist.get(position).isUsed = !tasklist.get(position).isUsed;
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    private boolean getWareList(Set<String> mWareList, String name) {
        for (String ware : mWareList) {
            if (ware.equals(name)) {
                return false;
            }
        }
        return true;
    }
}